package com.example.soa2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.soa2.common.CommonResponse;
import com.example.soa2.entity.Record;
import com.example.soa2.persistence.RecordMapper;
import com.example.soa2.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Service("RecordService")
public class RecordServiceImpl implements RecordService {
    @Autowired
    private RecordMapper recordMapper;

    @Override
    public CommonResponse<Record> getRecordById(int id) {
        return CommonResponse.createForSuccess(recordMapper.selectById(id));
    }

    @Override
    public CommonResponse<List<Record>> getRecords() {
        return CommonResponse.createForSuccess(recordMapper.selectList(null));
    }

    @Override
    public CommonResponse<List<Record>> getRecordsByPersonId(int personid) {
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("personid",personid);
        return CommonResponse.createForSuccess(recordMapper.selectList(queryWrapper));
    }

    @Override
    public CommonResponse<List<Record>> getRecordsByThingId(int thingid) {
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("thingid",thingid);
        return CommonResponse.createForSuccess(recordMapper.selectList(queryWrapper));
    }

    @Override
    public CommonResponse<String> postRecords(Record record) {
        int post = recordMapper.insert(record);
        if (post>0){
            return CommonResponse.createForSuccess(String.valueOf(post),"新增成功");
        }else{
            return CommonResponse.createForError("新增错误");
        }
    }

    @Override
    public CommonResponse<String> deleteById(int id) {
        int result = recordMapper.deleteById(id);
        if (result > 0){
            return CommonResponse.createForSuccess("删除成功");
        }else{
            return CommonResponse.createForError("删除失败");
        }
    }

    @Override
    public CommonResponse<String> deleteByIds(List<Integer> ids) {
        CommonResponse<String> commonResponse = CommonResponse.createForSuccess("删除成功");
        for (Integer id: ids) {
            commonResponse = deleteById(id);
            if (commonResponse.getStatus() == 1){
                return commonResponse;
            }
        }
        return commonResponse;
    }

    @Override
    public CommonResponse<String> updateRecord(int id, Record updatedRecord) {
        try {
            Record existingRecord = recordMapper.selectById(id);

            if (existingRecord == null) {
                return CommonResponse.createForError("更新失败，记录不存在");
            }

            // Perform the update operation based on the fields you want to update
            existingRecord.setPersonid(updatedRecord.getPersonid());
            existingRecord.setThingid(updatedRecord.getThingid());
            existingRecord.setNum(updatedRecord.getNum());
            existingRecord.setBegintime(updatedRecord.getBegintime());
            existingRecord.setEndtime(updatedRecord.getEndtime());
            existingRecord.setDetails(updatedRecord.getDetails());

            int updateResult = recordMapper.updateById(existingRecord);

            if (updateResult > 0) {
                return CommonResponse.createForSuccess("更新成功");
            } else {
                return CommonResponse.createForError("更新失败");
            }
        } catch (Exception e) {
            return CommonResponse.createForError("更新失败：" + e.getMessage());
        }
    }
}
